package cn.tqfeiyang.smartadmin.business.organization.service;

import cn.tqfeiyang.smartadmin.business.organization.entity.EmployeeEntity;
import cn.tqfeiyang.smartadmin.business.organization.mapper.EmployeeMapper;
import cn.tqfeiyang.smartadmin.business.organization.request.EmployeeCreateDTO;
import cn.tqfeiyang.smartadmin.business.organization.request.EmployeeUpdateDTO;
import cn.tqfeiyang.smartadmin.business.organization.request.EmployeeQueryDTO;
import cn.tqfeiyang.smartadmin.business.organization.response.EmployeeQueryVO;
import cn.tqfeiyang.smartadmin.business.organization.response.EmployeeDetailVO;

import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import jakarta.annotation.Resource;

import cn.tqfeiyang.smartadmin.commons.exception.BusinessException;
import cn.tqfeiyang.smartadmin.commons.toolbox.BeanUtils;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.mybatis.EntityBroadcast;
import cn.tqfeiyang.smartadmin.commons.toolbox.ValidationUtils;

import java.util.List;
import java.util.Objects;

/**
 * 员工CRUD服务
 *
 * @author tqfeiyang
 * @since 2024-10-18
 */
@Service
public class EmployeeCurdService {

    /**
     * 实体广播对象
     */
    @Resource
    private EntityBroadcast entityBroadcast;

    /**
     * 员工映射接口
     */
    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 分页查询员工
     *
     * @param dto 请求数据
     * @return 员工分页
     */
    public PageData<EmployeeQueryVO> queryEmployees(EmployeeQueryDTO dto) {
        Assert.notNull(dto, "dto不能为空");
        ValidationUtils.validateProperties(dto);

        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return PageData.create(employeeMapper.queryEmployees(dto));
    }

    /**
     * 查询员工详情
     *
     * @param employeeId 员工id
     * @return 员工详情
     */
    public EmployeeDetailVO readEmployee(Long employeeId) {
        Assert.notNull(employeeId, "employeeId不能为空");
        return employeeMapper.readEmployee(employeeId);
    }

    /**
     * 添加员工
     *
     * @param dto 请求数据
     * @return 员工id
     */
    @Transactional(rollbackFor = Exception.class)
    public Long createEmployee(EmployeeCreateDTO dto) {
        Assert.notNull(dto, "dto不能为空");
        ValidationUtils.validateProperties(dto);

        EmployeeEntity entity = BeanUtils.copy(dto, EmployeeEntity.class);
        entityBroadcast.validate(entity, true);

        employeeMapper.insert(entity);
        entityBroadcast.clearCache(EmployeeEntity.class);
        return entity.getEmployeeId();
    }

    /**
     * 修改员工
     *
     * @param employeeId 员工id
     * @param dto 请求数据
     * @return 员工id
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateEmployee(Long employeeId, EmployeeUpdateDTO dto) {
        Assert.notNull(employeeId, "employeeId不能为空");
        Assert.notNull(dto, "dto不能为空");
        ValidationUtils.validateProperties(dto);

        EmployeeEntity entity = employeeMapper.selectById(employeeId);
        if (Objects.isNull(entity)) {
            throw new BusinessException("员工id不存在({})", employeeId);
        }

        entity = BeanUtils.copy(dto, EmployeeEntity.class);
        entity.setEmployeeId(employeeId);
        entityBroadcast.validate(entity, false);

        employeeMapper.updateById(entity);
        entityBroadcast.clearCache(EmployeeEntity.class);
    }

    /**
     * 删除员工
     *
     * @param employeeId 员工id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteEmployee(Long employeeId) {
        Assert.notNull(employeeId, "employeeId不能为空");

        EmployeeEntity entity = employeeMapper.selectById(employeeId);
        if (Objects.isNull(entity)) {
            throw new BusinessException("员工id不存在({})", employeeId);
        }

        entityBroadcast.beforeDelete(entity);
        employeeMapper.deleteEmployee(employeeId);
        entityBroadcast.afterDelete(entity);
        entityBroadcast.clearCache(EmployeeEntity.class);
    }

    /**
     * 批量删除员工
     *
     * @param employeeIds 员工id列表
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchDeleteEmployees(List<Long> employeeIds) {
        Assert.notNull(employeeIds, "employeeIds不能为空");
        employeeIds.forEach(employeeId -> {
            EmployeeEntity entity = employeeMapper.selectById(employeeId);
            if (Objects.isNull(entity)) {
                throw new BusinessException("员工id不存在({})", employeeId);
            }

            entityBroadcast.beforeDelete(entity);
            employeeMapper.deleteEmployee(employeeId);
            entityBroadcast.afterDelete(entity);
        });
        entityBroadcast.clearCache(EmployeeEntity.class);
    }
}
